#!/bin/bash
# https://docs.docker.com/engine/install/binaries/
# https://download.docker.com/linux/static/stable/x86_64/docker-27.1.2.tgz
# prepare iptables,procps,xz utils
DOCKER_COMPRESS_FILE="docker-27.1.2.tgz"
INSTALL_PATH="/usr/local/bin"
SRC_PATH="/usr/local/src"
DOCKER_HTTP_PROXY="http://192.168.50.5:7890"
INSECURE_REGISTRY="192.168.50.8"

color() {
    local message="$1"
    local status="$2"
    local terminal_width
    terminal_width=$(tput cols)
    local middle_column=$(( terminal_width / 2))
    local SETCOLOR_SUCCESS="\033[1;32m"  # 绿色
    local SETCOLOR_FAILURE="\033[1;31m"  # 红色
    local SETCOLOR_WARNING="\033[1;33m"  # 黄色
    local SETCOLOR_NORMAL="\033[0m"      # 重置颜色
    printf '%s\n\033[%sG' "${message}"  "${middle_column}"
    # Print the status message
    printf "["
    case ${status} in
        success|0)
            echo -en  "${SETCOLOR_SUCCESS}  OK  "
            ;;
        failure|1)
            echo -en  "${SETCOLOR_FAILURE}FAILED"
            ;;
        *)
            echo -en "${SETCOLOR_WARNING}WARNING"
            ;;
    esac
    echo -e "${SETCOLOR_NORMAL}]"
}
check_user () {
    if [ "$(id -u)" = 0 ];then
        color "当前执行脚本的用户为管理员用户" 0
    else
        color "当前执行脚本的用户为非管理员用户，需要管理员用户执行此脚本!" 1
        exit
    fi
}
check_docker_file () {
    if [ -e ${DOCKER_COMPRESS_FILE} ];then
        color "当前目录下已找到docker二进制压缩包，正在解压到执行目录..." 0
        tar -xf $DOCKER_COMPRESS_FILE -C $SRC_PATH
        chown -R root:root $SRC_PATH/docker/*
        \cp -a $SRC_PATH/docker/* $INSTALL_PATH/
    else
        color "当前目录下没有找到docker二进制压缩包，请先将压缩包放到当前目录下" 1
        exit
    fi
}



config_service_file () {
        cat > /etc/systemd/system/docker.service <<-EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
Environment="HTTP_PROXY=${DOCKER_HTTP_PROXY}"
Environment="HTTPS_PROXY=${DOCKER_HTTP_PROXY}"

ExecStart=${INSTALL_PATH}/dockerd -H unix:///var/run/docker.sock --insecure-registry ${INSECURE_REGISTRY}
ExecReload=/bin/kill -s HUP \$MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target
EOF
}
start_docker_service () {
    if ! command -v iptables &> /dev/null;then
        apt update && apt install -y iptables
    fi
    systemctl daemon-reload
    systemctl enable --now docker && color "docker启动成功" 0
}
main () {
    check_user
    check_docker_file
    config_service_file
    start_docker_service
}

main
